// JavaScript Document
$(function () {
    init()



    function init(){
        $("#theme").click()
        refreshPersonalInfo();
        refreshRPInfo()
        refreshCLLsit()
    }
    function refreshCLLsit() {
        var uid=$("#uid").val()

        $.ajax({
            type:"post",
            data:{uid:uid},
            url:"http://127.0.0.1:8080/get_CLListByUid",
            success:function(data){
                // alert(data.msg)
                var str=""
                $("#CLLsitTableInput").html(str)
                if(data.status==1){
                    $("#CLLsitTableInput").append("<tr><td colspan='9'>暂无记录</td></tr>")
                }else {
                    for(var i=0;i<data.data.length;i++) {
                        str = ' <tr >\n' +
                            '<td >'+data.data[i].clid+'</td>\n' +
                            '<td>'+data.data[i].type+'</td>\n' +
                            '<td>'+data.data[i].start_time+'</td>\n' +
                            '<td>'+data.data[i].end_time+'</td>\n' +
                            '<td>'+data.data[i].detail+'</td>\n' +
                            '<td>'+data.data[i].signUp_time+'</td>\n' +
                            '<td>'+data.data[i].pass_time+'</td>\n'
                        switch (data.data[i].status) {
                            case 0: str+=  '<td class="status0"><b>未审批</b></td><td><a name="CLLsitDelete" class="btn pill fill">撤销</a> </td>\n'
                                break;
                            case 1:str+=  '<td class="status1"><b>通过</b></td><td></td>\n'
                                break;
                            case 2:str+=  '<td class="status2"><b>未通过</b></td><td><a name="CLLsitDelete" class="btn pill fill">删除</a> </td>\n'
                                break;
                            case 3:str+=  '<td class="status3"><b>审批超时</b></td><td><a name="CLLsitDelete" class="btn pill fill">删除</a> </td>\n'
                                break;
                        }

                        str+=  '</tr>'
                        $("#CLLsitTableInput").append(str)
                    }
                }


            },

            error:function () {
                alert("错误");
            }
        });
    }

    function refreshRPInfo() {
        var uid=$("#uid").val()

        $.ajax({
            type:"post",
            data:{uid:uid},
            url:"http://127.0.0.1:8080/get_RPInfoByUid",
            success:function(data){
                // alert(data.msg)
                var str=""
                $("#RPInfoTableInput").html(str)
                if(data.status==1){
                    $("#RPInfoTableInput").append("<tr><td colspan='4'>暂无记录</td></tr>")
                }else {
                    for(var i=0;i<data.data.length;i++) {
                        str = '<tr >\n' +
                            '<td >'+data.data[i].rpid+'</td>\n' +
                            '<td>'+data.data[i].type+'</td>\n' +
                            '<td>'+data.data[i].detail+'</td>\n' +
                            '<td>'+data.data[i].time.substring(0,10)+'</td>\n' +
                            '</tr>'
                        $("#RPInfoTableInput").append(str)
                    }
                }


            },

            error:function () {
                alert("错误");
            }
        });
    }
    function refreshPersonalInfo() {
        var uid=$("#uid").val()

        $.ajax({
            type:"post",
            data:{uid:uid},
            url:"http://127.0.0.1:8080/get_staffInfo",
            success:function(data){
               // alert(data.msg)
                var str=""
                $("#personalInfo").html(str)
                str='<tr><td><b>员工ID：</b></td><td class="long"><p>'+data.data.uid+' </p></td></tr>\n' +
                    '                    <tr><td><b>姓名：</b></td><td class="long"><p>'+data.data.name+'</p></td></tr>\n'
                if(data.data.sex==1){
                    str+= '                    <tr><td><b>性别：</b></td><td class="long"><p>男</p></td></tr>\n'
                }else {
                    str+= '                    <tr><td><b>性别：</b></td><td class="long"><p>女</p></td></tr>\n'
                }

                  str+=  '                    <tr><td><b>电话：</b></td><td class="long"><p>'+data.data.phone+'</p></td></tr>\n' +
                    '                    <tr><td><b>住址：</b></td><td class="long"><p>'+data.data.address+'</p></td></tr>\n' +
                    '                    <tr><td><b>生日：</b></td><td class="long"><p>'+data.data.birthday.substring(0,10)+'</p></td></tr>\n' +
                    '                    <tr><td><b>加入时间：</b></td><td class="long"><p>'+data.data.join_time.substring(0,10)+'</p></td></tr>\n' +
                    '                    <tr><td><b>部门：</b></td><td class="long"><p>'+data.data.department+'</p></td></tr>\n' +
                    '                    <tr><td><b>职位：</b></td><td class="long"><p>'+data.data.position+'</p></td></tr>\n' +
                    '                    <tr><td><b>薪资：</b></td><td class="long"><p>'+data.data.salary+'</p></td></tr>\n' +
                    '                    <tr><td><b>是否为正<br>\n'
                if(data.data.is_regular==1){
                    str+= '式员工：</b></td><td class="long"><p>是</p></td></tr>'
                }else {
                    str+= '式员工：</b></td><td class="long"><p>不是</p></td></tr>'
                }
                $("#personalInfo").append(str)


                $.ajax({
                    type:"post",
                    data:{uid:uid},
                    url:"http://127.0.0.1:8080/get_driverInfo",
                    success:function(data){
                        //alert(data.msg)
                        if(data.status==0){//该账号身份为司机
                            str='<tr><td><b>分配线路：</b></td><td class="long"><p>'+data.data.road_number+'</p></td></tr>'+
                                '<tr><td><b>线路详情：</b></td><td class="long"><p>'+data.data.line+'</p></td></tr>'
                            $("#personalInfo").append(str)
                        }

                    },

                    error:function () {
                        alert("错误");
                    }
                });
            },

            error:function () {
                alert("错误");
            }
        });

    }
    $(document).on("click","a[name='CLLsitDelete']",function(){
        var clid=$($(this).parents('tr').children('td').get(0)).html()
        var text=$(this).html();
        //alert("删除"+clid)
        $.ajax({
            type:"post",
            data:{clid:clid},
            url:"http://127.0.0.1:8080/delete_StaffCL",
            success:function(data){
               // alert(data.msg)
                if(data.status==0){
                    alert(text+"成功")
                }else {
                    alert("操作出错")
                }
                init()
            },
            error:function () {
                alert("错误");
            }
        });
    })
    $("#CreateCLConfirm").on("click",function (){
        var uid=$("#uid").val()
        var startTime=$("#startTime").val()
        var endTime=$("#endTime").val()
        var CLtype=$("#CLtype").val()
        var CLDetail=$("#CLDetail").val()
        if(startTime==""||endTime==""||CLtype==""||CLDetail==""){
            alert("请填写完整信息！")
        }else if(checkTime(startTime)){
            alert("开始时间不能在当前时间之前！")
        }else if(checkStartEnd(startTime,endTime)){
            alert("开始时间不能在结束时间之后！")
        }else {
            $.ajax({
                type:"post",
                data:{uid:uid,startTime:startTime,endTime:endTime},
                url:"http://127.0.0.1:8080/check_CLTime",
                success:function(data){
                   if(data.status==0){
                       $.ajax({
                           type:"post",
                           data:{uid:uid,startTime:startTime,endTime:endTime,CLtype:CLtype,CLDetail:CLDetail},
                           url:"http://127.0.0.1:8080/add_StaffCL",
                           success:function(data){
                               alert(data.msg)
                               init()
                           },
                           error:function () {
                               alert("错误");
                           }
                       });
                   }else {
                       alert("时间段与已发起的审批冲突！")
                   }
                },
                error:function () {
                    alert("错误");
                }
            });

        }

    })
    function checkTime(value) {
        var myDate = new Date();
        var now = myDate.valueOf();
        var time = new Date(value).valueOf();
        if(now>time){
            return true;
        }else{
            return false;
        }
    }
    function checkStartEnd(value1,value2) {
       // var myDate = new Date();
        var start = new Date(value1).valueOf();
        var end = new Date(value2).valueOf();
        if(start>end){
            return true;
        }else{
            return false;
        }
    }
    $("#logout").on("click",function () {
        formpost("http://127.0.0.1:8080/show_login",{});
    })

    $("#mainRefresh").on("click",function (){
        init();
        alert("已刷新")
    })
    $("#updatePwd").on("click",function () {
        var username=$("#username").val()
        //alert("username:"+username)
        var judge = prompt("修改密码：");
        if (judge) {
            formpost("http://127.0.0.1:8080/update_StaffPwdbyUsername",{ 'username':username,'pwd':judge});
        } else {
            alert("已取消")
        }
    })
    $("#personalInfoManagement").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxPersonalInfo").attr("hidden",false)
    })
    $("#showRPInfo").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxRPlInfo").attr("hidden",false)
    })
    $("#CreateCL").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxCreateCL").attr("hidden",false)
    })
    $("#CLList").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxCLLsit").attr("hidden",false)
    })
    function formpost(url, postData) {
        var tempform = document.createElement("form");
        tempform.action = url;
        tempform.target = "_self";
        tempform.method = "post";
        tempform.style.display = "none";
        for (var x in postData) {
            var opt = document.createElement("textarea");
            opt.name = x;
            opt.value = postData[x];
            tempform.appendChild(opt);
        }
        document.body.appendChild(tempform);
        tempform.submit();
    }
})